package xyz.xuminghai.javafx_learn.demo.roll_call;

import javafx.animation.AnimationTimer;
import javafx.geometry.Pos;
import javafx.scene.control.Button;
import javafx.scene.layout.HBox;
import javafx.scene.layout.StackPane;
import javafx.scene.text.Text;

import java.util.ArrayList;
import java.util.List;

/**
 * 2023/12/9 22:14 星期六<br/>
 *
 * @author xuMingHai
 */
public class RollCallView extends HBox {

    private final StackPane nameStackPane = new StackPane();
    private final Button button = new Button("点名");

    private final List<Text> nameList;

    private AnimationTimer animationTimer;


    public RollCallView() {
        List<String> list = List.of("张一", "张二", "张三", "张四", "张五",
                "张六", "张七", "张八", "张九", "张十", "张十一", "张十二", "张十三", "张十四", "张十五",
                "张十六", "张十七", "张十八", "张十九", "张二十", "张二十一", "张二十二", "张二十三",
                "张二十四", "张二十五", "张二十六", "张二十七", "张二十八", "张二十九", "张三十",
                "张三十一", "张三十二", "张三十三", "张三十四", "张三十五", "张三十六", "张三十七",
                "张三十八", "张三十九", "张四十", "张四十一", "张四十二", "张四十三", "张四十四",
                "张四十五", "张四十六", "张四十七", "张四十八", "张四十九", "张五十");
        this.nameList = new ArrayList<>(list.size());
        list.forEach(name -> nameList.add(new Text(name)));

        buttonAction();
        super.setPrefSize(500.0, 200.0);
        super.setAlignment(Pos.CENTER);
        super.setSpacing(20.0);
        super.getChildren().addAll(nameStackPane, button);
    }

    private void buttonAction() {
        button.setOnAction(event -> getAnimationTimer().start());
    }

    private AnimationTimer getAnimationTimer() {
        if (animationTimer == null) {
            RollCallAnimation rollCallAnimation = new RollCallAnimation(nameStackPane.getChildren(), nameList);
            button.disableProperty().bind(rollCallAnimation.activeProperty());
            animationTimer = rollCallAnimation;
        }
        return animationTimer;
    }

}
